﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace WindowsPhoneGameHungTrung
{
    public abstract class Product
    {
        protected string _productName;

        private Vector2 _posRegionBasket;
        private Vector2 _whRegionBasket;

        private My2DSprite _productSprite;

        public My2DSprite ProductSprite
        {
            get { return _productSprite; }
            set { _productSprite = value; }
        }     

        public virtual double AutoMoveLevel_1()
        {
            Random r = new Random();
            return 2 * r.NextDouble();
        }

        public virtual double AutoMoveLevel_2()
        {
            Random r = new Random();
            return 4 * r.NextDouble();
        }

        public virtual double AutoMoveLevel_3()
        {
            Random r = new Random();
            return 8 * r.NextDouble();
        }

        public virtual double AutoMoveLevel_4()
        {
            Random r = new Random();
            return 16 * r.NextDouble();
        }

        public virtual double AutoMoveLevel_5()
        {
            Random r = new Random();
            return 32 * r.NextDouble();
        }

        public virtual double AutoMoveLevel_6()
        {
            Random r = new Random();
            return 64 * r.NextDouble();
        }

        private Vector2 updateVector;

        public bool IsEndFall = false;

        
        public void UpdateRegionBasket(Vector2 posRegion, Vector2 whRegion)
        {
            _posRegionBasket = posRegion;
            _whRegionBasket = whRegion;
        }

        public virtual void LoadContent(ContentManager content)
        {
            Texture2D[] tmpTexture;
            Vector2 tmpVector;

            // ro
            tmpTexture = new Texture2D[1];
            tmpTexture[0] = content.Load<Texture2D>(Global.PATH_FILE_IMAGE + _productName.ToLower());

            tmpVector = new Vector2(-100, 0);

            _productSprite = new My2DSprite(tmpTexture, tmpVector);          

            //font = content.Load<SpriteFont>(@"Font\Score");
        }

        public virtual void Update(GameTime gameTime)
        {
            float _speed = 0;
            switch (Global.LEVEL)
            {
                case 1:
                    _speed = (float)AutoMoveLevel_1();
                    break;
                case 2:
                    _speed = (float)AutoMoveLevel_2();
                    break;
                case 3:
                    _speed = (float)AutoMoveLevel_3();
                    break;
                case 4:
                    _speed = (float)AutoMoveLevel_4();
                    break;
                case 5:
                    _speed = (float)AutoMoveLevel_5();
                    break;
                case 6:
                    _speed = (float)AutoMoveLevel_5();
                    break;
            }

            updateVector = new Vector2(
                _productSprite.Position.X,
                _productSprite.Position.Y + _speed);

            //Delta = (float)AutoMoveLevel_1();
            //Delta = 1.0f;
            //if (Delta > MaxDelta)
            //    MaxDelta = Delta;

            //updateVector = new Vector2(
            //    _productSprite.Position.X,
            //    _productSprite.Position.Y + Delta);

            _productSprite.UpdatePosition(updateVector);

            if (IsCollisionBasket() == true)
            {
                switch (_productName)
                {
                    case "Egg":
                        Global.NUMBER_EGG++;
                        break;
                    case "Shit":
                        Global.NUMBER_SHIT++;
                        break;
                }
                IsEndFall = true;
            }

            if (IsCollisionRope() == true)
            {
                if (_productName == "Egg")
                    Global.NUMBER_HEART--;
                IsEndFall = true;
            }

            _productSprite.Update(gameTime);
        }

        public virtual void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            _productSprite.Draw(gameTime, spriteBatch);            
        }

        public abstract Product Clone(Vector2 posAnimal, Vector2 whAnimal);

        protected Vector2 CalculatorPos(Vector2 PositionAnimal, Vector2 WidthHeightAnimal)
        {            
            return new Vector2(PositionAnimal.X + WidthHeightAnimal.X / 2 - ProductSprite.getWidthTexture()/2,
                PositionAnimal.Y + WidthHeightAnimal.Y); 
        }

        public bool IsCollisionBasket()
        {
            Vector2 posPro = _productSprite.Position;
            Vector2 whPro = new Vector2(_productSprite.getWidthTexture(), _productSprite.getHeightTexture());

            // -------------
            // x           x
            if (posPro.X >= _posRegionBasket.X && posPro.X + whPro.X <= _posRegionBasket.X + _whRegionBasket.X)
            {
                if (posPro.Y + whPro.Y >= _posRegionBasket.Y - 10
                    && posPro.Y + whPro.Y <= _posRegionBasket.Y + 20)
                    return true;
            }
            return false;
        }

        public bool IsCollisionRope()
        {
            Vector2 posPro = _productSprite.Position;
            Vector2 whPro = new Vector2(_productSprite.getWidthTexture(), _productSprite.getHeightTexture());

            
            if (posPro.Y + whPro.Y >= Global.Y_DAY_DUOI)
                return true;
            
            return false;
        }
    }
}
